package org.amphiprion.rfreplayanalyser.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PreferenceApplication {
	static Properties prop = new Properties();
	static File propFile = new File(System.getProperty("user.home") + "/" + ".GRReplayTool/config.properties");
	static {
		propFile.getParentFile().mkdirs();
		try {
			prop.load(new FileInputStream(propFile));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static String read(String key, String defaultValue) {
		if (prop.containsKey(key)) {
			return prop.getProperty(key);
		} else {
			return defaultValue;
		}
	}

	public static float readFloat(String key, float defaultValue) {
		if (prop.containsKey(key)) {
			return Float.parseFloat(prop.getProperty(key));
		} else {
			return defaultValue;
		}
	}

	public static int readInt(String key, int defaultValue) {
		if (prop.containsKey(key)) {
			return Integer.parseInt(prop.getProperty(key));
		} else {
			return defaultValue;
		}
	}

	public static void write(String key, String value) {
		if (value != null) {
			prop.setProperty(key, value);
		} else {
			prop.setProperty(key, value == null ? "" : value);
		}
	}

	public static void writeFloat(String key, float value) {
		write(key, "" + value);
	}

	public static void writeInt(String key, int value) {
		write(key, "" + value);
	}

	public static void commit() {
		try {
			prop.store(new FileOutputStream(propFile), "Generated by GR Replay Tool");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
